// 定义模块
define(["jquery", "login", "register"], function($, LoginModal, RegisterModal) {
	function Header() {
		this.render();
		this.loadUser();
		this.addListener();
	}

	// 头部导航布局HTML文本
	Header.headerTemplate = `<nav class="navbar navbar-inverse">
	    <!-- Brand and toggle get grouped for better mobile display -->
	    <div class="navbar-header">
	      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
	        <span class="sr-only">Toggle navigation</span>
	        <span class="icon-bar"></span>
	        <span class="icon-bar"></span>
	        <span class="icon-bar"></span>
	      </button>
	      <a class="navbar-brand" href="#">拉勾网管理系统</a>
	    </div>

	    <!-- Collect the nav links, forms, and other content for toggling -->
	    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
	      <ul class="nav navbar-nav">
	        <li class="active"><a href="/">首页</a></li>
	        <li class="management"><a href="/htm/position.html">职位管理</a></li>
	      </ul>

	      <ul class="nav navbar-nav navbar-right not-loged-in">
	        <li  data-toggle="modal" data-target="#loginModal"><a href="#">登录</a></li>
	        <li  data-toggle="modal" data-target="#registerModal"><a href="#">注册</a></li>
				</ul>
				<ul class="nav navbar-nav navbar-right loged-in hidden">
				<li><a href="#">欢迎：</a></li>
				<li><a href="#" class="link-logout">退出</a></li>
			</ul>
	    </div><!-- /.navbar-collapse -->
	  </nav>`;

	$.extend(Header.prototype, {
		// 渲染头部布局结构
		render() {
			$(Header.headerTemplate).appendTo("header");
		},
		//加载用户信息
		loadUser() {
			// 判断是否有用户登录
			const user = sessionStorage.loginUser;
			if (user) { // 有登录成功的用户，则显示欢迎
				$(".loged-in").removeClass("hidden").siblings(".not-loged-in").remove();
				$(".loged-in a:first").text("欢迎：" + user);
			} else {
				this.loadModal();
			}
		},
		// 加载模态框
		loadModal(){
		   new LoginModal();
			 new RegisterModal();	 
		},
		// 注册事件监听
		addListener() {
			$(".link-logout").click(()=>{
				sessionStorage.removeItem("loginUser");
				location.reload();
			});
		}
	});

	return new Header();
});

